﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class ToolDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.Tool.GetAll);
		}

		public static DataSet GetAllDS()
		{
			return ExcuteProcedureDS(ToolConstants.StoreProcedure.Tool.GetAll);
		}

		public static DataTable GetOne(string toolID)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@ToolID", SqlDbType.NVarChar, toolID)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Tool.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static Tools LoadOne(string toolID)
		{
			Tools o = new Tools();
			DataTable dt = GetOne(toolID);
			if(dt.Rows.Count > 0)
			{
				o.ToolID = dt.Rows[0][ToolConstants.Entities.Tool.FieldName.ToolID].ToString();
				o.ToolName = dt.Rows[0][ToolConstants.Entities.Tool.FieldName.ToolName].ToString();
				o.MakerToolName = dt.Rows[0][ToolConstants.Entities.Tool.FieldName.MakerToolName].ToString();
                o.Price = !Utilities.IsNullOrEmpty(dt.Rows[0][ToolConstants.Entities.Tool.FieldName.Price].ToString()) ? decimal.Parse(dt.Rows[0][ToolConstants.Entities.Tool.FieldName.Price].ToString()) : 0;
				o.FrequencyID = dt.Rows[0][ToolConstants.Entities.Tool.FieldName.FrequencyID].ToString();
				o.ToolGroupID = dt.Rows[0][ToolConstants.Entities.Tool.FieldName.ToolGroupID].ToString();
                o.MakerID = dt.Rows[0][ToolConstants.Entities.Tool.FieldName.MakerID].ToString();
				o.SafeQty = !Utilities.IsNullOrEmpty(dt.Rows[0][ToolConstants.Entities.Tool.FieldName.SafeQty].ToString()) ? int.Parse(dt.Rows[0][ToolConstants.Entities.Tool.FieldName.SafeQty].ToString()) : 0;

			}

			return o;
		}

		public static int Insert(Tools o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ToolID", SqlDbType.NVarChar, o.ToolID),
                        CreateParameter("@ToolName", SqlDbType.NVarChar, o.ToolName),
                        CreateParameter("@MakerToolName", SqlDbType.NVarChar, o.MakerToolName),
                        CreateParameter("@Price", SqlDbType.Decimal, o.Price),
                        CreateParameter("@FrequencyID", SqlDbType.NVarChar, o.FrequencyID),
                        CreateParameter("@ToolGroupID", SqlDbType.NVarChar, o.ToolGroupID),
                        CreateParameter("@MakerID", SqlDbType.NVarChar, o.MakerID),
                        CreateParameter("@SafeQty", SqlDbType.Int, o.SafeQty),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Tool.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static int Update(Tools o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@ToolID", SqlDbType.NVarChar, o.ToolID),
                        CreateParameter("@ToolName", SqlDbType.NVarChar, o.ToolName),
                        CreateParameter("@MakerToolName", SqlDbType.NVarChar, o.MakerToolName),
                        CreateParameter("@Price", SqlDbType.Decimal, o.Price),
                        CreateParameter("@FrequencyID", SqlDbType.NVarChar, o.FrequencyID),
                        CreateParameter("@ToolGroupID", SqlDbType.NVarChar, o.ToolGroupID),
                        CreateParameter("@MakerID", SqlDbType.NVarChar, o.MakerID),
                        CreateParameter("@SafeQty", SqlDbType.Int, o.SafeQty),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Tool.Update, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static int Delete(Tools o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ToolID", SqlDbType.NVarChar, o.ToolID)
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Tool.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("333_Tool");

			return table;
		}

		public static DataTable GetAllLeftJoin()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.Tool.GetAllLeftJoin);
		}

		public static DataTable GetOneLeftJoin(string toolId)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@ToolID", SqlDbType.NVarChar, toolId)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Tool.GetOneLeftJoin, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static DataTable GetAllByToolType()
		{
			try
			{
				return ExcuteProcedure(ToolConstants.StoreProcedure.Tool.GetAllByToolType);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}
		#endregion
	}
}
